package com.itheima.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Component//交由spring容器
@Aspect//声明为切面类
@EnableAspectJAutoProxy //开启aop注解驱动
public class AOPAdvice {
    //拦截业务层的以Service为后缀的接口
    @Pointcut("execution(* com.itheima.service.*Service.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //执行签名信息
        Signature signature = pjp.getSignature();
        //通过签名获取执行类型
        String targetClass = signature.getDeclaringTypeName();
        //通过签名获取执行方法名
        String targetMethod = signature.getName();

        Long beginTime = System.currentTimeMillis();
        //执行原有的方法,有参数通过pjp对象获取
        Object obj = pjp.proceed(pjp.getArgs());

        Long endTime = System.currentTimeMillis();
        System.out.println(targetClass+" 中 "+targetMethod+" 运行时长 "+(endTime-beginTime)+"ms");
        return obj;
    }
}
